{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "List Comprehension.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyOpyXggwdWs8rCTxr3OHb4l",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/EvgSkv/logica/blob/main/examples/more/List_Comprehension.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "L0tJR2GmRUI1"
      },
      "source": [
        "# Example of List Comprehension in Logica"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Dd-cWY8UPWSC",
        "outputId": "f0e594f1-6dc8-4fbf-c70d-124ee2ff9852"
      },
      "source": [
        "!git clone -b dev https://github.com/evgskv/logica"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'logica'...\n",
            "remote: Enumerating objects: 1131, done.\u001b[K\n",
            "remote: Counting objects: 100% (323/323), done.\u001b[K\n",
            "remote: Compressing objects: 100% (193/193), done.\u001b[K\n",
            "remote: Total 1131 (delta 186), reused 187 (delta 128), pack-reused 808\u001b[K\n",
            "Receiving objects: 100% (1131/1131), 3.50 MiB | 19.24 MiB/s, done.\n",
            "Resolving deltas: 100% (625/625), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PMTmimLtPXnT"
      },
      "source": [
        "from logica import colab_logica \n",
        "colab_logica.SetTabulatedOutput(False)"
      ],
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 215
        },
        "id": "cA9Ym6zJPaF2",
        "outputId": "510a7c7b-fd36-4587-a547-145df6d9d46b"
      },
      "source": [
        "%%logica PremiumPurchases\n",
        "\n",
        "@Engine(\"sqlite\");\n",
        "\n",
        "Purchases(customer: \"John\",                                                                                                                          \n",
        "          tickets: [{price: 10, tier: \"economy\"},{price: 20, tier: \"premium\"}]);                                                                     \n",
        "Purchases(customer: \"James\",                                                                                                                         \n",
        "          tickets: [{price: 5, tier: \"economy\"}]);                                                                                                   \n",
        "                                                                                                                                                     \n",
        "PremiumPurchases(customer:, premium_tickets:, premium_total:) :-\n",
        "  Purchases(customer:, tickets:),                                                                                                                  \n",
        "  premium_tickets List= (                                                                                                                          \n",
        "    ticket :- ticket in tickets, ticket.tier == \"premium\"                                                                                          \n",
        "  ),                                                                                                                                               \n",
        "  premium_total += (ticket.price :- ticket in premium_tickets),                                                                                    \n",
        "  Size(premium_tickets) > 0;    "
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Query is stored at \u001b[1mPremiumPurchases_sql\u001b[0m variable.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<graphviz.dot.Digraph at 0x7fdf9cf161d0>"
            ],
            "image/svg+xml": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n -->\n<!-- Title: Concertina Pages: 1 -->\n<svg width=\"238pt\" height=\"44pt\"\n viewBox=\"0.00 0.00 237.79 44.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 40)\">\n<title>Concertina</title>\n<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-40 233.7893,-40 233.7893,4 -4,4\"/>\n<!-- PremiumPurchases -->\n<g id=\"node1\" class=\"node\">\n<title>PremiumPurchases</title>\n<path fill=\"#caff70\" stroke=\"#575757\" d=\"M103.0395,-34.141C103.0395,-34.141 11.9604,-19.859 11.9604,-19.859 6.0328,-18.9295 6.0328,-17.0705 11.9604,-16.141 11.9604,-16.141 103.0395,-1.859 103.0395,-1.859 108.9671,-.9295 120.8222,-.9295 126.7498,-1.859 126.7498,-1.859 217.8289,-16.141 217.8289,-16.141 223.7565,-17.0705 223.7565,-18.9295 217.8289,-19.859 217.8289,-19.859 126.7498,-34.141 126.7498,-34.141 120.8222,-35.0705 108.9671,-35.0705 103.0395,-34.141\"/>\n<text text-anchor=\"middle\" x=\"114.8947\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">PremiumPurchases</text>\n</g>\n</g>\n</svg>\n"
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Running predicate: PremiumPurchases\n",
            "The following table is stored at \u001b[1mPremiumPurchases\u001b[0m variable.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>customer</th>\n",
              "      <th>premium_tickets</th>\n",
              "      <th>premium_total</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>John</td>\n",
              "      <td>[{\"price\":20,\"tier\":\"premium\"}]</td>\n",
              "      <td>20</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "  customer                  premium_tickets  premium_total\n",
              "0     John  [{\"price\":20,\"tier\":\"premium\"}]             20"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            " \n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}